#!/bin/bash

function site_move() {
	OUTDIR=/vagrant/output/upgrade/$PLATNEW
	if [ ! -d $OUTDIR ]
	then
		mkdir -p $OUTDIR
	fi
	
	OLDDIR=/var/www/$PLATOLD
	NEWDIR=/var/www/$PLATNEW
	
	if [ ! -d $OLDDIR ]
	then
		echo "Old platform $OLDDIR not found"
		return 1
	fi
	
	if [ ! -d $NEWDIR ]
	then
		echo "New platform $NEWDIR not found"
		return 2
	fi
	
	echo "Moving site $SITENAME from platform $PLATOLD to $PLATNEW"	
	mv $OLDDIR/sites/$SITENAME $NEWDIR/sites/$SITENAME
}

function vhost_update() {
	echo "Changing vhost and ssl"
	CONFPLAIN="/etc/apache2/sites-available/$SITENAME"
	CONFSSL="$CONFPLAIN-ssl"

	for CONF in $CONFPLAIN $CONFSSL
	do
		sed -i "s/$PLATOLD/$PLATNEW/g" $CONF
	done
}

function drush_update() {
    echo "Drush update"
	cd $NEWDIR/sites/$SITENAME
	drush -y updatedb > $OUTDIR/$SITENAME.log 2>&1
}


if [ $# -eq 3 ]
then
	SITENAME=$1
	PLATOLD=$2
	PLATNEW=$3
	
	site_move
	vhost_update
	drush_update
else
	echo "Usage: site_upgrade.sh <fqdn_site> <old_platform_name> <new_platform_name>"
	exit -1
fi
